Skip to content

Conversation

@matteobettini
Copy link
Contributor

Description

Making _set_seed abstract

Motivation and Context

In the current implementation of EnvBase there is a convention for certain methods to have and abstract counterpart.

step() -> _step()
reset() -> _reset()

The same seems to be valid for set_seed(), except that _set_seed() is not officially abstract.

Following the convention it would be intuitive that inheritors of EnvBase would override _set_seed(), but there are currenty some inheritors which do not implement _set_seed(). This breaks the intuitive convention.

In this PR we made _set_seed() abstract. This caused some inheritors which override set_seed() (like _BatchedEnv and TransformedEnv) to need to implement _set_seed() with no action (as these classes act as wrappers).

Types of changes

What types of changes does your code introduce? Remove all that do not apply:

  • Bug fix (non-breaking change which fixes an issue)

Checklist

Go over all the following points, and put an x in all the boxes that apply.
If you are unsure about any of these, don't hesitate to ask. We are here to help!

  • I have read the CONTRIBUTION guide (required)
  • My change requires a change to the documentation.
  • I have updated the tests accordingly (required for a bug fix or a new feature).
  • I have updated the documentation accordingly.

I have checked that the tests in test_env.py which I am able to run (without external libraries and cuda) are all passing and linted.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 28, 2022
@codecov
Copy link

codecov bot commented Dec 28, 2022

Codecov Report

Merging #770 (c403278) into main (1ce4fc5) will decrease coverage by 0.01%.
The diff coverage is 60.00%.

@@            Coverage Diff             @@
##             main     #770      +/-   ##
==========================================
- Coverage   88.75%   88.73%   -0.02%     
==========================================
  Files         123      123              
  Lines       20987    20986       -1     
==========================================
- Hits        18626    18622       -4     
- Misses       2361     2364       +3     
Flag Coverage Δ
habitat-gpu 24.98% <75.00%> (+<0.01%) ⬆️
linux-brax 29.58% <75.00%> (+<0.01%) ⬆️
linux-cpu 85.27% <60.00%> (-0.02%) ⬇️
linux-gpu 86.17% <60.00%> (-0.02%) ⬇️
linux-jumanji 30.36% <75.00%> (+<0.01%) ⬆️
linux-outdeps-gpu 71.91% <60.00%> (+0.01%) ⬆️
linux-stable-cpu 85.12% <60.00%> (-0.02%) ⬇️
linux-stable-gpu 85.83% <60.00%> (-0.02%) ⬇️
linux_examples-gpu 42.95% <75.00%> (+<0.01%) ⬆️
macos-cpu 84.95% <60.00%> (-0.02%) ⬇️
olddeps-gpu 75.91% <60.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
test/mocking_classes.py 97.25% <42.85%> (-0.63%) ⬇️
torchrl/envs/transforms/transforms.py 87.29% <66.66%> (-0.07%) ⬇️
torchrl/envs/vec_env.py 69.37% <75.00%> (-0.07%) ⬇️
torchrl/envs/common.py 83.50% <100.00%> (+0.17%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Collaborator

@vmoens vmoens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Let's merge main in this branch to resolve the conflicts

def set_seed(
self, seed: Optional[int] = None, static_seed: bool = False
) -> Optional[int]:
# This method is not used in transformed environments
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this true? The message matches the one from _set_seed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could use the docstring above?

Another note: I don't remember why we're not using _set_seed to be honest. I guess it's because the seed iteration would occur twice if transformed_env._set_seed would call ˋbase_env.set_seed, but what if transformed_env._set_seed would call ˋbase_env._set_seed?

Copy link
Contributor Author

@matteobettini matteobettini Dec 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this true? The message matches the one from _set_seed

Oh sorry it was not meant to be present in set_seed. Fixed

Maybe we could use the docstring above?

Yes, fixed

Another note: I don't remember why we're not using _set_seed to be honest. I guess it's because the seed iteration would occur twice if transformed_env._set_seed would call ˋbase_env.set_seed, but what if transformed_env._set_seed would call ˋbase_env._set_seed?

Basically _set_seed is only called in the original implementation of set_seed in BaseEnv. Given that TransformedEnv and BatchedEnv override set_seed (this would not normally be the case for inheritors of BaseEnv and it is the case just for these two examples as they are wrappers which need to change the seeding logic) the inner _set_seed is not called anymore and we just implement it with a pass (whatever we implement it with doesn't matter as it is never called)


def _set_seed(self, seed: Optional[int]):
# This method is not used in batched envs
return
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe pass instead of return? Seems semantically more appropriate to me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep will fix

@matteobettini
Copy link
Contributor Author

All the comments should have been addressed

@vmoens vmoens merged commit 578938a into pytorch:main Dec 29, 2022
@matteobettini matteobettini deleted the seeding branch December 29, 2022 22:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants